import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/home/index',
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children:[
      {
        path:'index',
        component: () => import('../views/Index.vue')
      },
      {
        //创意家居
        path:'furnishings',
        component: () => import('../views/Category/Furnishings.vue'),
        meta: {
          thumbs: ['商品分类', '创意家居']
        }
      },
      {
        //时尚饰品
        path:'jewelry',
        component: () => import('../views/Category/Jewelry.vue'),
        meta: {
          thumbs: ['商品分类', '时尚饰品']
        }
      },
      {
        //生活百货
        path:'dailynecessities',
        component: () => import('../views/Category/Dailynecessities.vue'),
        meta: {
          thumbs: ['商品分类', '时尚饰品']
        }
      },
      {
        //数码配件
        path:'electron',
        component: () => import('../views/Category/Electron.vue'),
        meta: {
          thumbs: ['商品分类', '数码配件']
        }
      },
      {
        //健康美容
        path:'cosmetology',
        component: () => import('../views/Category/Cosmetology.vue'),
        meta: {
          thumbs: ['商品分类', '健康美容']
        }
      },
      {
        //食品
        path:'food',
        component: () => import('../views/Category/Food.vue'),
        meta: {
          thumbs: ['商品分类', '食品']
        }
      },
      {
        //增加商铺
        path:'shoppingadd',
        component: () => import('../views/Category/ShoppingAdd.vue'),
        meta: {
          thumbs: ['商品分类', '增加商铺']
        }
      },
      {
      //店铺新增
        path: 'shop-add',
        component: () => import('../views/shop/ShopAdd.vue'),
        meta: {
          thumbs: ['店铺管理', '店铺新增']
        }
      },
      {
        //店铺列表
        path: 'shop-list',
        component: () => import('../views/shop/ShopList.vue'),
        meta: {
          thumbs: ['店铺管理', '店铺列表']
        }
      },
      {
        //店铺列表
        path: 'admin',
        component: () => import('../views/Admin/Admin.vue'),
        meta: {
          thumbs: ['店铺管理', '店铺列表']
        }
      },
    ]
  },
  {
    path: '/user/login',
    name: 'login',
    component: () => import('../views/user/Login.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

import store from '../store'

// 设置全局前置守卫  每当路由跳转时执行 （在显示目标组件之前执行）
router.beforeEach((to, from, next)=>{
  // 向后继续执行，显示登录页面
  // 不是访问登录，就需要验证vuex用户
  if(to.path=='/user/login' || store.state.user){ 
    next() 
  }else {
    router.push('/user/login')
  }
})



export default router
